package com.zmn.oms.services.impl.dict;

import com.zmn.oms.model.entity.dict.ChannelZhimiFault;
import com.zmn.oms.persistence.interfaces.dict.ChannelZhimiFaultDAO;
import com.zmn.oms.services.interfaces.dict.ChannelZhimiFaultService;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sunlife
 * @date:2020/5/9 11:51 上午
 * description:
 */
@Service
public class ChannelZhimiFaultServiceImpl implements ChannelZhimiFaultService {

    @Resource
    private ChannelZhimiFaultDAO channelZhimiFaultDAO;

    @Override
    public List<ChannelZhimiFault> listByProductCode(String productCode) {
        ChannelZhimiFault query = new ChannelZhimiFault();
        query.setProductCode(productCode);
        return channelZhimiFaultDAO.query(query);
    }

    @Override
    public List<ChannelZhimiFault> listByThirdModelCode(String thirdModelCode) {
        if (thirdModelCode == null) {
            return new ArrayList<>();
        }

        return channelZhimiFaultDAO.listByThirdModelCode(thirdModelCode);
    }

    @Override
    public void saveList(List<ChannelZhimiFault> faultList) {

        // 删除数据库中已有数据
        channelZhimiFaultDAO.deleteAll();

        if (faultList.size() <= 300) {
            channelZhimiFaultDAO.insertBatch(faultList);
        } else {
            List<List<ChannelZhimiFault>> partition = ListUtils.partition(faultList, 300);
            partition.forEach(items -> channelZhimiFaultDAO.insertBatch(items));
        }
    }

}
